package com.example.petstore_2.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.petstore_2.common.CommonResponse;
import com.example.petstore_2.domain.Product;
import com.example.petstore_2.mapper.ProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("productService")
public class ProductService {

    @Autowired
    private ProductMapper productMapper;
    public CommonResponse getProductList() {
        List<Product> productList = productMapper.selectList(null);
        return CommonResponse.creatForSuccess(productList);
    }

    public CommonResponse getProductListByCategory(String category) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category", category);
        List<Product> productList = productMapper.selectList(queryWrapper);
        System.out.println(productList);
        return CommonResponse.creatForSuccess(productList);
    }

    public CommonResponse getProductListByKey(String keyWord) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyWord);
        List<Product> productList = productMapper.selectList(queryWrapper);
//        System.out.println(productList);
        log.info("用户搜索" + keyWord);
        return CommonResponse.creatForSuccess(productList);
    }

    public List<String> getProductIdList() {
        List<Product> productList = productMapper.selectList(null);
        List<String> productIdList = new ArrayList<>();
    //    System.out.println(productList);
        for (Product product : productList) {
            productIdList.add(product.getProductid());
        }

        return productIdList;
    }

    public CommonResponse<Product> getProductByProductId(String productId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("productid", productId);
//        productMapper.selectList(queryWrapper);
        return CommonResponse.creatForSuccess(productMapper.selectOne(queryWrapper));
//        return ;

    }
}
